package com.data.algorithm.sort;

/**
 * 快速排序
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/3/24
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] arr = new int[8000000];

        for (int i = 0; i < 8000000; ++i) {
            arr[i] = (int) (Math.random() * 80000000.0D);
        }

        Long startTime = System.currentTimeMillis();
        System.out.println("开始排序");
        quickSort(arr,0,arr.length-1);
        Long endTime = System.currentTimeMillis() - startTime;

        System.out.println("排序耗时：" + endTime + "毫秒");
        System.out.println("排序耗时：" + endTime / 1000 + "秒");
    }

    /**
     * 快速排序
     *
     * @param
     * @param left
     * @param right
     */
    public static void quickSort(int[] arr, int left, int right) {
        int l = left;
        int r = right;
        int pivot = arr[(left + right) / 2];
        boolean var6 = false;

        while(l < r) {
            while(arr[l] < pivot) {
                ++l;
            }

            while(arr[r] > pivot) {
                --r;
            }

            if (l >= r) {
                break;
            }

            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            if (arr[l] == pivot) {
                --r;
            }

            if (arr[r] == pivot) {
                ++l;
            }
        }

        if (l == r) {
            ++l;
            --r;
        }

        if (left < r) {
            quickSort(arr, left, r);
        }

        if (right > l) {
            quickSort(arr, l, right);
        }

    }
}
